/**
 * 
 */
package org.dg.iag.server;

import java.util.Map;
import java.util.logging.Logger;

import org.dg.iag.dao.GenericDAO;
import org.dg.iag.entity.Log;
import org.dg.iag.entity.Logs;
import org.dg.iag.interf.entity.GenericRestResource;
import org.dg.iag.query.XQueryGet;
import org.dg.iag.query.XQueryUtil;
import org.dg.iag.util.ApplicationConstants;
import org.dg.iag.util.RestletUtil;
import org.restlet.resource.ServerResource;

/**
 * @author Alex Gartner
 *
 */
public class LogsServerResource extends ServerResource implements
		GenericRestResource<Logs> {

	Logger logger		= Logger.getLogger(LogsServerResource.class.getName() );
	@Override
	public Logs getElement() {
		logger.info("Getting logs from db");
		Map<String,String> filterMap	= RestletUtil.getFiltersFromServerResource( this );
		GenericDAO<Log, Logs> gdao		= new GenericDAO<Log, Logs>(ApplicationConstants.LOGS_XML_FOLDER);
		
		if ( filterMap.get("timeMillis") == null || "last".equals(filterMap.get("timeMillis")) ) {
			XQueryGet xqg			= new XQueryGet("", "max(/Logs/Log/timeMillis)", false);
			
			String result			= gdao.getElementsAsString(xqg);
			long resultLong			= Double.valueOf(result).longValue();
			filterMap.put("timeMillis", resultLong+"");
		}
		
		String query					= "let $ret := /Logs/Log" + XQueryUtil.generateXpathCondition(filterMap, true) + 
				" return <Logs>{$ret}</Logs> ";
		XQueryGet xqg					= new XQueryGet("", query , false);
		Logs ret						= gdao.getElements(LogsResource.class, xqg);
		
		
		return ret;
	}

	@Override
	public void addElement(Logs element) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Object modifyElement(Logs element) {
		// TODO Auto-generated method stub
		return null;
	}

	public interface LogsResource extends GenericRestResource<Logs> {};
	public interface LogResource extends GenericRestResource<Log> {};
}
